using LF.Common;
using LF.Domain.Entities;
using LF.Domain.Helper;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LF.Domain.Services.Iml
{
    public class KnowledgeBaseManagerService : IKnowledgeBaseManagerService
    {
        private readonly ILogger<KnowledgeBaseManagerService> _logger;

        public KnowledgeBaseManagerService(ILogger<KnowledgeBaseManagerService> logger)
        {
            _logger = logger;
        }

        #region 知识库管理
        /// <summary>
        /// 获取知识库列表
        /// </summary>
        public List<KnowledgeBase> GetKnowledgeBaseList(int index, int pageSize, string? name, bool? active)
        {
            var query = FreeSqlHelper.DbContext.Queryable<KnowledgeBase>()
                .Where(p => !p.IsDelete);

            if (!string.IsNullOrEmpty(name))
                query = query.Where(p => p.Name.Contains(name));

            if (active.HasValue)
                query = query.Where(p => p.Active == active.Value);

            var list = query.OrderByDescending(p => p.CreatTime)
                .Skip((index - 1) * pageSize)
                .Take(pageSize)
                .ToList();

            return list;
        }

        /// <summary>
        /// 获取知识库总数
        /// </summary>
        public long CountKnowledgeBase(string? name, bool? active)
        {
            var query = FreeSqlHelper.DbContext.Queryable<KnowledgeBase>()
                .Where(p => !p.IsDelete);

            if (!string.IsNullOrEmpty(name))
                query = query.Where(p => p.Name.Contains(name));

            if (active.HasValue)
                query = query.Where(p => p.Active == active.Value);

            return query.Count();
        }

        /// <summary>
        /// 获取知识库详情
        /// </summary>
        public KnowledgeBase GetKnowledgeBase(Guid id)
        {
            var item = FreeSqlHelper.DbContext.Queryable<KnowledgeBase>()
                .Where(p => p.Id == id)
                .First();
            return item;
        }

        /// <summary>
        /// 新增知识库
        /// </summary>
        public bool InsertKnowledgeBase(KnowledgeBase knowledgeBase)
        {
            var result = FreeSqlHelper.DbContext.Insert(knowledgeBase).ExecuteAffrows();
            return result > 0;
        }

        /// <summary>
        /// 更新知识库
        /// </summary>
        public bool UpdateKnowledgeBase(KnowledgeBase knowledgeBase)
        {
            var result = FreeSqlHelper.DbContext.Update<KnowledgeBase>(knowledgeBase.Id)
                .SetSource(knowledgeBase)
                .ExecuteAffrows();
            return result > 0;
        }

        /// <summary>
        /// 删除知识库
        /// </summary>
        public bool DeleteKnowledgeBase(Guid id)
        {
            var result = FreeSqlHelper.DbContext.Delete<KnowledgeBase>(id).ExecuteAffrows();
            return result > 0;
        }

        /// <summary>
        /// 获取所有知识库
        /// </summary>
        public List<KnowledgeBase> GetAllKnowledgeBase()
        {
            var list = FreeSqlHelper.DbContext.Queryable<KnowledgeBase>()
                .Where(p => !p.IsDelete)
                .OrderByDescending(p => p.CreatTime)
                .ToList();
            return list;
        }
        #endregion

        #region 知识库文件管理
        /// <summary>
        /// 获取知识库文件列表
        /// </summary>
        public List<KnowledgeFile> GetKnowledgeFileList(int index, int pageSize, Guid? knowledgeBaseId, string? fileName, string? suffix)
        {
            var query = FreeSqlHelper.DbContext.Queryable<KnowledgeFile>();

            if (knowledgeBaseId.HasValue)
                query = query.Where(p => p.KnowledgeBaseId == knowledgeBaseId.Value);

            if (!string.IsNullOrEmpty(fileName))
                query = query.Where(p => p.FileName.Contains(fileName));

            if (!string.IsNullOrEmpty(suffix))
                query = query.Where(p => p.Suffix.Contains(suffix));

            var list = query.OrderByDescending(p => p.CreatTime)
                .Skip((index - 1) * pageSize)
                .Take(pageSize)
                .ToList();

            return list;
        }

        /// <summary>
        /// 获取知识库文件总数
        /// </summary>
        public long CountKnowledgeFile(Guid? knowledgeBaseId, string? fileName, string? suffix)
        {
            var query = FreeSqlHelper.DbContext.Queryable<KnowledgeFile>();

            if (knowledgeBaseId.HasValue)
                query = query.Where(p => p.KnowledgeBaseId == knowledgeBaseId.Value);

            if (!string.IsNullOrEmpty(fileName))
                query = query.Where(p => p.FileName.Contains(fileName));

            if (!string.IsNullOrEmpty(suffix))
                query = query.Where(p => p.Suffix.Contains(suffix));

            return query.Count();
        }

        /// <summary>
        /// 获取知识库文件详情
        /// </summary>
        public KnowledgeFile GetKnowledgeFile(Guid id)
        {
            var item = FreeSqlHelper.DbContext.Queryable<KnowledgeFile>()
                .Where(p => p.Id == id)
                .First();
            return item;
        }

        /// <summary>
        /// 新增知识库文件
        /// </summary>
        public bool InsertKnowledgeFile(KnowledgeFile knowledgeFile)
        {
            knowledgeFile.CreatTime = DateTime.Now;
            var result = FreeSqlHelper.DbContext.Insert(knowledgeFile).ExecuteAffrows();
            return result > 0;
        }

        /// <summary>
        /// 更新知识库文件
        /// </summary>
        public bool UpdateKnowledgeFile(KnowledgeFile knowledgeFile)
        {
            var result = FreeSqlHelper.DbContext.Update<KnowledgeFile>(knowledgeFile.Id)
                .SetSource(knowledgeFile)
                .ExecuteAffrows();
            return result > 0;
        }

        /// <summary>
        /// 删除知识库文件
        /// </summary>
        public bool DeleteKnowledgeFile(Guid id)
        {
            var result = FreeSqlHelper.DbContext.Delete<KnowledgeFile>(id).ExecuteAffrows();
            return result > 0;
        }
        #endregion
    }
} 